//
//  ScanCodeViewController.m
//  LvBao
//
//  Created by 范琦 on 16/6/6.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "ScanCodeViewController.h"
#import "OrderDetailTopCell.h"
#import "OrderDetailCell.h"
#import "ScanCodeSectionCell.h"
#import "ScanCodeCell.h"
#import "ScanCodeEnterViewController.h"

@interface ScanCodeViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *oneArr;
@property (nonatomic, strong) NSMutableArray *twoArr;

@property (nonatomic, strong) NSMutableArray *byArr;
@property (nonatomic, assign) NSInteger i;


@end

@implementation ScanCodeViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.i = 1;
    [self creatTableView];
    
    self.oneArr = [NSMutableArray arrayWithObjects:@"xxx玩偶", @"xxx玩偶", @"xxx玩偶", @"xxx玩偶", @"xxx玩偶", nil];
    self.byArr = [NSMutableArray arrayWithObjects:@"xxx玩偶", @"xxx玩偶", @"xxx玩偶", @"xxx玩偶", @"xxx玩偶", nil];
    self.twoArr = [NSMutableArray array];
    
}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"OrderDetailTopView" bundle:nil] forCellReuseIdentifier:@"cell1"];
    [_tableView registerNib:[UINib nibWithNibName:@"ScanCodeCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
    [_tableView registerNib:[UINib nibWithNibName:@"ScanCodeSectionCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    _tableView.sectionFooterHeight = 0;
    _tableView.backgroundColor = [UIColor huiseColor];
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 100)];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(30, 40, KSCREENWIDTH - 60, 40);
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    button.tintColor = [UIColor whiteColor];
    button.layer.cornerRadius = 20;
    button.layer.masksToBounds = YES;
    [view addSubview:button];
    [button addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];
    _tableView.tableFooterView = view;
    
}

- (void)enter
{
    ScanCodeEnterViewController *enter = [[ScanCodeEnterViewController alloc]init];
    [self.navigationController pushViewController:enter animated:YES];


}
#warning 加减按钮
- (void)reduce:(UIButton *)button
{
    ScanCodeCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:button.tag % 1000 inSection:button.tag / 1000]];
    NSInteger i = [cell.textFiled.text integerValue];
    cell.textFiled.text = [NSString stringWithFormat:@"%lu", i - 1];

    

}

- (void)add:(UIButton *)button
{
    ScanCodeCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:button.tag % 1000 inSection:button.tag / 1000]];
    NSInteger i = [cell.textFiled.text integerValue];
    cell.textFiled.text = [NSString stringWithFormat:@"%lu", i + 1];
    
    

    
    
}

#warning tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return self.oneArr.count + 1;
    }
    return self.twoArr.count + 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 100;
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            return 40;
        }
        return 50;
    }else{
        if (indexPath.row == 0) {
            return 40;
        }
        return 50;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        
        return 1;
    }else
    {
    
        return 20;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        OrderDetailTopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.section == 1){
        
        if (indexPath.row == 0) {
            ScanCodeSectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell.titleLabel.text = @"电子垃圾";
            
            return cell;
        }
        
        ScanCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
     
        cell.reduce.tag = 1000 * indexPath.section + indexPath.row;
        cell.add.tag = 1000 * indexPath.section + indexPath.row;
        [cell.reduce addTarget:self action:@selector(reduce:) forControlEvents:UIControlEventTouchUpInside];
        [cell.add addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];


        return cell;
    }
    else{
        if (indexPath.row == 0) {
            ScanCodeSectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell.titleLabel.text = @"废品垃圾";
            return cell;
        }
        ScanCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        
        cell.reduce.tag = 1000 * indexPath.section + indexPath.row;
        cell.add.tag = 1000 * indexPath.section + indexPath.row;
        [cell.reduce addTarget:self action:@selector(reduce:) forControlEvents:UIControlEventTouchUpInside];
        [cell.add addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];

        return cell;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 1) {
        
        if (_oneArr.count == 0) {
            _oneArr = [NSMutableArray arrayWithArray:_byArr];
            _byArr = [NSMutableArray arrayWithArray:_twoArr];
            [_twoArr removeAllObjects];
            [tableView reloadData];
        }
        
    }else if (indexPath.section == 2){
        if (_twoArr.count == 0) {
            
            _twoArr = [NSMutableArray arrayWithArray:_byArr];
            _byArr = [NSMutableArray arrayWithArray:_oneArr];
            [_oneArr removeAllObjects];
            [tableView reloadData];
        }
        
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
